/**
 * Project Name:HomeActivity
 * Date:2016-5-25下午2:54:50
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.db.user.model;

import android.content.ContentValues;

import com.kkd.kkdapp.db.annotate.EADBField;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldMode;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldType;
import com.kkd.kkdapp.db.app.EADBHelper;
import com.kkd.kkdapp.db.base.EABaseModel;
import com.kkd.kkdapp.db.user.KkdDBHelper;
import com.kkd.kkdapp.http.entity.InformationDataList;

/**
 * ClassName: KkdMessageModel <br/>
 * date: 2016-5-25 下午2:54:50 <br/>
 * 消息中心的系统消息
 * @author askzhao
 * @version 
 */
public class KkdMessageModel extends EABaseModel {
	
	private static final String TABLE_NAME = KkdMessageModel.class.getSimpleName();
	public static final int CONTAINED = -2016;//表明已经存在该消息

	@EADBField(mode = EADBFieldMode.NotNull,type = EADBFieldType.Text )
	public String id;
	@EADBField(type = EADBFieldType.Text)
	public String title;
	@EADBField(type = EADBFieldType.Text)
	public String publishDate;
	@EADBField(type = EADBFieldType.Text)
	public String description;
	@EADBField(type = EADBFieldType.Text)
	public String status = "未读";
	@EADBField(type = EADBFieldType.Integer)
	public long createTime;
	
	public static long insert(InformationDataList msg) {
		KkdMessageModel curMsg = KkdDBHelper.getInstance().queryTopOne(KkdMessageModel.class, " id=? ",
				new String[] { msg.getId() });
		if(curMsg!=null){
			return CONTAINED;
		}
		KkdMessageModel newMsg = new KkdMessageModel();		
		newMsg.id = msg.getId();
		newMsg.title = msg.getTitle();
		newMsg.publishDate = msg.getPublishDate();
		newMsg.description = msg.getDescription();
		if(msg.getStatus()==null){
			newMsg.status = "未读";
		}else{
			newMsg.status = msg.getStatus();
		}
		
		newMsg.createTime = msg.getCreateTime();
		return KkdDBHelper.getInstance().saveBindId(newMsg);
	}
	
	public static boolean setAllReaded() {
		ContentValues values = new ContentValues();
		values.put("status", "已读");
		return KkdDBHelper.getInstance().update(TABLE_NAME, values, "",
				new String[] {  }) > 0;
	}
	
	public static boolean updateStatusById(String status,String id) {
		ContentValues values = new ContentValues();
		values.put("status", status);
		return KkdDBHelper.getInstance().update(TABLE_NAME, values, " id=? ",
				new String[] { id }) > 0;
	}

	/**
	 * findOneOrderInfo:(这里用一句话描述这个方法的作用). <br/>
	 * @author askzhao
	 * @param id2
	 * @return
	 */
	public static InformationDataList findOneOrderInfo(String id) {
		KkdMessageModel newMsg = KkdDBHelper.getInstance().queryTopOne(KkdMessageModel.class, " id=? ",
				new String[] { id });
		if(newMsg!=null){
			InformationDataList msg = new InformationDataList();
			msg.setId(newMsg.id);
			msg.setTitle(newMsg.title);
			msg.setPublishDate(newMsg.publishDate);
			msg.setDescription(newMsg.description);
			msg.setStatus(newMsg.status);
			msg.setCreateTime(newMsg.createTime);
			return msg;	
		}else{
			return null;
		}
		
	}
	
}
